package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.HyrjMachineDisinfectionMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HyrjReportMapper;

/**
 * 举报管理，用于记录和处理用户的举报Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@Service
public class HyrjReportServiceImpl implements IHyrjReportService {
    @Autowired
    private HyrjReportMapper hyrjReportMapper;
    @Autowired
    private IHyrjMachineService hyrjMachineService;
    @Autowired
    private IHyrjAppointmentActionService hyrjAppointmentActionService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IHyrjUseMachineService hyrjUseMachineService;
    @Autowired
    private HyrjMachineDisinfectionMapper hyrjMachineDisinfectionMapper;


    /**
     * 查询举报管理，用于记录和处理用户的举报
     *
     * @param id 举报管理，用于记录和处理用户的举报主键
     * @return 举报管理，用于记录和处理用户的举报
     */
    @Override
    public HyrjReport selectHyrjReportById(Long id) {
        return hyrjReportMapper.selectHyrjReportById(id);
    }

    /**
     * 查询举报管理，用于记录和处理用户的举报列表
     *
     * @param hyrjReport 举报管理，用于记录和处理用户的举报
     * @return 举报管理，用于记录和处理用户的举报
     */
    @Override
    public List<HyrjReport> selectHyrjReportList(HyrjReport hyrjReport) {
        return hyrjReportMapper.selectHyrjReportList(hyrjReport);
    }

    /**
     * 新增举报管理，用于记录和处理用户的举报
     *
     * @param hyrjReport 举报管理，用于记录和处理用户的举报
     * @return 结果
     */
    @Override
    public int insertHyrjReport(HyrjReport hyrjReport) {
        HyrjMachine hyrjMachine = hyrjMachineService.selectHyrjMachineByNo(hyrjReport.getMachineNo());
        if (null != hyrjMachine) {
            HyrjUseMachine hyrjUseMachine = new HyrjUseMachine();
            hyrjUseMachine.setMachineNo(hyrjMachine.getMachineNo());
            HyrjUseMachine hyrjUseMachine1 = hyrjUseMachineService.selectHyrjUseMachineByNewUseTime(hyrjUseMachine);
            if (null != hyrjUseMachine1) {
                hyrjReport.setReportedPersonId(hyrjUseMachine1.getUserId());
                hyrjReport.setReportTime(new Date());
                hyrjReport.setReportStatus("1");
            }
        }
        return hyrjReportMapper.insertHyrjReport(hyrjReport);
    }

    /**
     * 修改举报管理，用于记录和处理用户的举报
     *
     * @param hyrjReport 举报管理，用于记录和处理用户的举报
     * @return 结果
     */
    @Override
    public int updateHyrjReport(HyrjReport hyrjReport) {
        if (hyrjReport.getReportStatus().equals("2")) {
            SysUser sysUser = userService.selectUserById(hyrjReport.getReportedPersonId());
            if (null != sysUser) {
                Integer reportNumber = null == sysUser.getReportNumber() ? 0 : sysUser.getReportNumber();
                sysUser.setReportNumber(reportNumber + 1);
                userService.updateUser(sysUser);
            }
            HyrjAppointmentAction hyrjAppointmentAction = new HyrjAppointmentAction();
            hyrjAppointmentAction.setUserId(hyrjReport.getReportedPersonId());
            List<HyrjAppointmentAction> hyrjAppointmentActions = hyrjAppointmentActionService.selectHyrjAppointmentActionList(hyrjAppointmentAction);
            if (!hyrjAppointmentActions.isEmpty()) {
                String machineNo = hyrjAppointmentActions.get(0).getMachineNo();
                HyrjMachine hyrjMachine = hyrjMachineService.selectHyrjMachineByNo(machineNo);
                if (null != hyrjMachine) {
                    hyrjMachine.setMachineStatus("4");
                    hyrjMachineService.updateHyrjMachine(hyrjMachine);
                }
                HyrjMachineDisinfection machineDisinfection = new HyrjMachineDisinfection();
                machineDisinfection.setMachineNo(machineNo);
                machineDisinfection.setRemarks("被举报有违禁品");
                machineDisinfection.setDisinfectionStatus("1");
                hyrjMachineDisinfectionMapper.insertHyrjMachineDisinfection(machineDisinfection);
            }
            hyrjReport.setAuditTime(new Date());


        }
        return hyrjReportMapper.updateHyrjReport(hyrjReport);
    }

    /**
     * 批量删除举报管理，用于记录和处理用户的举报
     *
     * @param ids 需要删除的举报管理，用于记录和处理用户的举报主键
     * @return 结果
     */
    @Override
    public int deleteHyrjReportByIds(Long[] ids) {
        return hyrjReportMapper.deleteHyrjReportByIds(ids);
    }

    /**
     * 删除举报管理，用于记录和处理用户的举报信息
     *
     * @param id 举报管理，用于记录和处理用户的举报主键
     * @return 结果
     */
    @Override
    public int deleteHyrjReportById(Long id) {
        return hyrjReportMapper.deleteHyrjReportById(id);
    }
}
